//
//  DropDownMenuView.m
//  QRMaster
//
//  Created by myApple on 15/4/14.
//  Copyright (c) 2015年 myApple. All rights reserved.
//

#import "DropDownMenuView.h"

@interface DropDownMenuView()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView * DropDownMenuTabelView;
    NSMutableArray * DropDownMenuArray;
    NSInteger selectIndex;//选中第几项
}

@end

@implementation DropDownMenuView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        selectIndex = 10000;//默认是没有的
         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(removeTableViewFromSuperview:) name:@"moveWindown" object:nil];
        DropDownMenuArray = [[NSMutableArray alloc]init];
        [self prepareView];
        UITapGestureRecognizer * tapView = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(removeView)];
        [self addGestureRecognizer:tapView];
    }
    return self;
}

-(void)removeView
{
    self.hidden = YES;
}

-(void)prepareView
{
    float ScreenWidth = self.frame.size.width;
    float x= self.frame.origin.x;
    float y= self.frame.origin.y;
    float width = ScreenWidth;
    float height = 0;
    
    DropDownMenuTabelView = [[UITableView alloc]initWithFrame:CGRectMake(x, y, width, height) style:UITableViewStylePlain];
    DropDownMenuTabelView.delegate = self;
    DropDownMenuTabelView.dataSource = self;
    DropDownMenuTabelView.tableFooterView = [[UIView alloc]init];
    DropDownMenuTabelView.separatorColor = RGBA(237, 238, 241, 1);
    [self addSubview:DropDownMenuTabelView];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return DropDownMenuArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString * string = @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:string];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
    }
    
    if (_data.count > indexPath.row) {
        cell.textLabel.text = DropDownMenuArray[indexPath.row];
        cell.textLabel.font = [UIFont systemFontOfSize:14.0];
        if (selectIndex == indexPath.row) {
            cell.textLabel.textColor = UIColorFromRGB(0x7dc234);
        }
        else
        {
            cell.textLabel.textColor = [UIColor blackColor];
        }
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    selectIndex = indexPath.row;
    if (_delegate && [_delegate respondsToSelector:@selector(DropDownMenuView:showIndex:back:)]) {
        [_delegate DropDownMenuView:self showIndex:(int)indexPath.row back:NO];
        [UIView animateWithDuration:0.5 animations:^{
            CGRect frame = DropDownMenuTabelView.frame;
            frame.size.height = 0;
            DropDownMenuTabelView.frame = frame;
        } completion:^(BOOL finished) {
            DropDownMenuTabelView.hidden = YES;
        }];
        [self showOrNotList:YES];
    }
}

-(void)showOrNotList:(BOOL)flag
{
     float ScreenHeight = 0;
    if (_data.count <= 4) {
        ScreenHeight = _data.count * 40;
    }
    else
        ScreenHeight = 160;
   
    if (flag) {
        [UIView animateWithDuration:0.5 animations:^{
            CGRect frame = DropDownMenuTabelView.frame;
            frame.size.height = 0;
            DropDownMenuTabelView.frame = frame;
        } completion:^(BOOL finished) {
            DropDownMenuTabelView.hidden = YES;
        }];
    }
    else
    {
        DropDownMenuTabelView.hidden = NO;
        [self.window addSubview:DropDownMenuTabelView];
        [UIView animateWithDuration:0.5 animations:^{
            CGRect frame = DropDownMenuTabelView.frame;
            frame.size.height = ScreenHeight;
            DropDownMenuTabelView.frame = frame;
        } completion:^(BOOL finished) {
        }];
    }
}

-(void)setData:(NSMutableArray *)data
{
    _data = data;
    DropDownMenuArray = _data;
    
    [DropDownMenuTabelView reloadData];
}

-(void)removeTableViewFromSuperview:(NSNotification *)not
{
    [DropDownMenuTabelView removeFromSuperview];
}



-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"moveWindown" object:nil];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
